package com.qingxin.park.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.park.entity.Crops;
import com.qingxin.park.entity.ParkBlock;
import com.qingxin.park.entity.ParkBlockPlant;
import com.qingxin.park.entity.ParkBlockPlantView;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 地块种植信息
 * @author Administrator
 *
 */
@Service
public class ParkBlockPlantService extends BaseService<ParkBlockPlant, String> {
	
	@Autowired
	private ParkBlockService parkBlockService;
	@Autowired
	private CropsService cropsService;
	
	
	/**
	 * 种植信息列表分页数据
	 * @param parkid
	 * @param blockid
	 * @param begin
	 * @param end
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String parkid, String blockid, String plantType, Date begin, Date end, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.park.entity.ParkBlockPlantView(" +
				"p.plantid, p.cropName, p.beginTime, p.endTime, p.isCurrent, p.plantStatus, p.blockName, p.isuse)" +
				" from ParkBlockPlantView p where p.isdel='0' and p.plantType=:plantType ");
		querier.setParam("plantType", plantType);
		
		if(StringUtils.isNotEmpty(parkid)){
			querier.append(" and p.parkid=:parkid");
			querier.setParam("parkid", parkid);
			
			if(!SystemCfg.isDataAdminUser(loginUser)){
				querier.append(" and p.blockid in (select distinct v.parkBlock.blockid from SysUserBlockRel v where v.sysUser.userid=:userid)");
				querier.setParam("userid", loginUser.getUserid());
			}
		}else{
			querier.append(" and p.blockid=:blockid");
			querier.setParam("blockid", blockid);
		}
		
		if(begin != null){
			querier.append(" and p.beginTime >= :beginTime ");
			querier.setParam("beginTime", begin);
		}
		if(end != null){
			querier.append(" and p.beginTime <= :endTime ");
			querier.setParam("endTime", end);
		}
		
		querier.append(pageParam.getOrderby(" order by p.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<ParkBlockPlantView> listForApp(String parkName, String blockName, String plantType, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from ParkBlockPlantView p where p.isdel='0' and p.plantType=:plantType ");
		querier.setParam("plantType", plantType);
		
		if(StringUtils.isNotEmpty(parkName)){
			querier.append(" and p.parkName like '%"+parkName.replace("'", "")+"%'");
		}
		if(StringUtils.isNotEmpty(blockName)){
			querier.append(" and p.blockName like '%"+blockName.replace("'", "")+"%'");
		}
		
		if(!SystemCfg.isDataAdminUser(loginUser)){
			querier.append(" and p.blockid in (select distinct v.parkBlock.blockid from SysUserBlockRel v where v.sysUser.userid=:userid)");
			querier.setParam("userid", loginUser.getUserid());
		}
		
//		if(StringUtils.isNotEmpty(blockName)){
//			querier.append(" and p.blockName like '%"+blockName+"%'");
//		}else if(StringUtils.isNotEmpty(parkName)){
//			querier.append(" and p.parkName like '%"+parkName+"%'");
//			
//			if(!SystemCfg.isDataAdminUser(loginUser)){
//				querier.append(" and p.blockid in (select distinct v.parkBlock.blockid from SysUserBlockRel v where v.sysUser.userid=:userid)");
//				querier.setParam("userid", loginUser.getUserid());
//			}
//		}else {
//			if(!SystemCfg.isDataAdminUser(loginUser)){
//				querier.append(" and p.blockid in (select distinct v.parkBlock.blockid from SysUserBlockRel v where v.sysUser.userid=:userid)");
//				querier.setParam("userid", loginUser.getUserid());
//			}
//		}
		
		querier.append(pageParam.getOrderby(" order by p.createTime desc"));
		List<ParkBlockPlantView> list = listByQuerier(querier, pageParam);
		return list;
	}
	
	
	
	/**
	 * 获取指定地块的当前种植信息
	 * @param blockid
	 * @return
	 */
	public ParkBlockPlant getCurrentPlant(String blockid){
		ParkBlockPlant plant = (ParkBlockPlant) getByHql("from ParkBlockPlant p" +
				" where p.parkBlock.blockid=? and p.isCurrent='1' order by beginTime desc", blockid);
		return plant;
	}
	
	/**
	 * 获取指定地块的当前种植信息
	 * @param blockid
	 * @return
	 */
	public ParkBlockPlantView getCurrentPlantView(String blockid){
		ParkBlockPlantView plant = (ParkBlockPlantView) getByHql("from ParkBlockPlantView p" +
				" where p.blockid=? and p.isCurrent='1' order by beginTime desc", blockid);
		return plant;
	}
	
	
	/**
	 * 获取指定种植信息
	 * @param plantid
	 * @return
	 */
	public ParkBlockPlantView getPlantView(String plantid){
		ParkBlockPlantView plant = (ParkBlockPlantView) getByHql("from ParkBlockPlantView p where p.plantid=?", plantid);
		return plant;
	}
	
	
	/**
	 * 添加种植信息
	 * @param plant
	 * @param blockid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(ParkBlockPlant plant, String blockid, LoginUser loginUser, HttpServletRequest request){
		ParkBlock block = parkBlockService.get(blockid);
		if(block == null){
			return new SysError("参数错误");
		}
		plant.setParkBlock(new ParkBlock(blockid));
		
		plant.setPlantType(block.getBlockType());
		
		//添加新种植信息时将该地块已有种植信息的状态改为历史状态
		updateByHql("update ParkBlockPlant p set p.isCurrent=0,p.plantStatus=1 where p.parkBlock.blockid=?", blockid);
		
		
		Date now = new Date();
		plant.setCreateTime(now);
		plant.setUpdateTime(now);
		plant.setCreateBy(loginUser.getLogin_name());
		plant.setUpdateBy(loginUser.getLogin_name());
		
		plant.setIsdel("0");
		
		this.save(plant);
		return null;
	}
	
	
	/**
	 * 修改种植信息
	 * @param plant
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(ParkBlockPlant plant, LoginUser loginUser, HttpServletRequest request){
		ParkBlockPlant old = getSafeEntity(plant.getPlantid());
		if(old==null){
			return new SysError("参数错误");
		}
		Crops crops = cropsService.get(old.getPlantid());
		if(crops != null ){
			plant.setCropName(crops.getCropName());
		}else {
			plant.setCropName(old.getCropName());
		}
		
		String fields = "cropName,beginTime,endTime,isCurrent,plantStatus,remark";
		BeanUtil.setTargetFields(old, plant, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<ParkBlockPlant> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除种植信息
	 * @param plantids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] plantids, LoginUser loginUser, HttpServletRequest request){
		for(String plantid : plantids){
			ParkBlockPlant plant = get(plantid);
			if(plant==null){
				continue;
			}
			
			this.delete(plant);
		}
		
		return null;
	}
	
	
	
	
	
}
